package movingbox;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class BoxContainer extends JScrollPane implements Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Thread t;
	JPanel jp;
	
	final List<PacketBox> boxes;

	public BoxContainer(List<PacketBox> boxes){

		t = new Thread(this, "Box Container");
		
		boxes = new ArrayList<PacketBox>();
		
		this.boxes = boxes;

		init();

	}

	public void init(){
		
		jp = new JPanel();
		
		jp.setLayout(null);
		
		this.setViewportView(jp);
		
		this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		//this.getVerticalScrollBar().setBlockIncrement(16);

		//try{
			boxes.add(new PacketBox());
			//currentItem = 0;
			boxes.get(0).setIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_0.png"), MovingBox.currentD.width, MovingBox.currentD.height));
			boxes.get(0).setRolloverIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_1.png"), MovingBox.currentD.width, MovingBox.currentD.height));
			MovingBox.removeBoxBorder(boxes.get(0));
			boxes.get(0).setBounds(MovingBox.startP.x, MovingBox.currentP.y, MovingBox.currentD.width, MovingBox.currentD.height);
			boxes.get(0).addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					
					MovingBox.wishItem = 0;
					
					if(MovingBox.currentItem == MovingBox.wishItem){
						// go stage4
					}else{
						MovingBox.beforeCurrentItem = MovingBox.currentItem;
						MovingBox.currentItem = MovingBox.wishItem;
						adjustAllBoxesSize();
						adjustAllBoxesLocation();
					}
					
				}
			});
			
			jp.add(boxes.get(0));
		//}Finally{

		//}
	}

	public void run() {

		// TODO Auto-generated method stub
		try{
			for(int i =0; i<30; i++){

				boxes.add(new PacketBox());
				setBoxBounds(boxes.get(boxes.size()-1));
				

				Thread.sleep(10);
				
				revalidate();
				repaint();
			}
			
			
		} catch (InterruptedException e){
			System.out.println("Child interrupted.");
		}
		System.out.println("Exiting child thread.");
	}
	
	
	public void setBoxBounds(final PacketBox box) throws InterruptedException{
		//setBoxSize(box);
		//setBoxLocation(box);
		
		if(boxes.indexOf(box) != 0){
			
			if(boxes.indexOf(box) == MovingBox.currentItem){

				box.setIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_0.png"), MovingBox.currentD.width, MovingBox.currentD.height));
				box.setRolloverIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_1.png"), MovingBox.currentD.width, MovingBox.currentD.height));
				MovingBox.removeBoxBorder(box);
			
				box.setBounds(boxes.get(boxes.indexOf(box)-1).getLocation().x + boxes.get(boxes.indexOf(box)-1).getSize().width,
						MovingBox.currentP.y,
						MovingBox.currentD.width, MovingBox.currentD.height);

			}else{
				
				box.setIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_0.png"), MovingBox.restD.width, MovingBox.restD.height));
				box.setRolloverIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_1.png"), MovingBox.restD.width, MovingBox.restD.height));
				MovingBox.removeBoxBorder(box);
				
				box.setBounds(boxes.get(boxes.indexOf(box)-1).getLocation().x + boxes.get(boxes.indexOf(box)-1).getSize().width,
						MovingBox.startP.y,
						MovingBox.restD.width, MovingBox.restD.height);
				
			}
			
		}
		
		box.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				MovingBox.wishItem = boxes.indexOf(box);
				
				if(MovingBox.currentItem == MovingBox.wishItem){
					// go stage4
				}else{
					MovingBox.beforeCurrentItem = MovingBox.currentItem;
					MovingBox.currentItem = MovingBox.wishItem;
					adjustAllBoxesSize();
					adjustAllBoxesLocation();
				}
				
			}
		});
		
		adjustAllBoxesLocation();
		
		jp.add(box);
		
		jp.setPreferredSize(new Dimension(MovingBox.currentD.width + (boxes.size()-1)*MovingBox.restD.width + (MovingBox.spaceD.width*2), 600));
		
		/*//animation
		int i = 1;
		
		while(!box.getSize().equals(MovingBox.currentD)){
			
			box.setSize(box.getSize().width+1, box.getSize().height+1);
			box.setIcon(scaleImageIcon(getImageIcon("/images/FTP_0.png"), box.getSize().width+1, box.getSize().height+1));
			box.setRolloverIcon(scaleImageIcon(getImageIcon("/images/FTP_1.png"), box.getSize().width+1, box.getSize().height+1));
			
			jp.setPreferredSize(new Dimension(boxes.size()*MovingBox.currentD.width, 600));
			
			revalidate();
			repaint();
			
			i++;
			//if(i>10) i = i/2;
			if(i<83) Thread.sleep(2);
			else Thread.sleep(30);
			
		}
		*/
		
		
	}

	public void setBoxLocation(PacketBox box){

		if(boxes.indexOf(box) != 0){

			box.setLocation(boxes.get(boxes.indexOf(box)-1).getLocation().x + boxes.get(boxes.indexOf(box)-1).getSize().width,
					boxes.get(boxes.indexOf(box)-1).getLocation().y + boxes.get(boxes.indexOf(box)-1).getSize().height);

			//revalidate();
			//repaint();
		}

		if(boxes.indexOf(box) == MovingBox.currentItem){
			//box.setLocation(0, 0);
		}else{

		}
	}

	public void setBoxSize(PacketBox box){
		if(boxes.indexOf(box) == MovingBox.currentItem){
			// animation
			box.setSize(MovingBox.restD);

			while(!box.getSize().equals(MovingBox.currentD)){
				box.setSize(box.getSize().width+1, box.getSize().height+1);
				//revalidate();
				//repaint();
			}

		}else{
			while(!box.getSize().equals(MovingBox.restD)){
				box.setSize(box.getSize().width-1, box.getSize().height-1);
				//revalidate();
				//repaint();
			}
		}
	}
	
public void adjustAllBoxesSize(){
		
		boxes.get(MovingBox.beforeCurrentItem).setIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_0.png"), MovingBox.restD.width, MovingBox.restD.height));
		boxes.get(MovingBox.beforeCurrentItem).setRolloverIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_1.png"), MovingBox.restD.width, MovingBox.restD.height));
		
		boxes.get(MovingBox.currentItem).setIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_0.png"), MovingBox.currentD.width, MovingBox.currentD.height));
		boxes.get(MovingBox.currentItem).setRolloverIcon(MovingBox.scaleImageIcon(MovingBox.getImageIcon("/images/FTP_1.png"), MovingBox.currentD.width, MovingBox.currentD.height));
}
	
	public void adjustAllBoxesLocation(){
		
		for(int i=0; i < boxes.size(); i++){
			if(i == 0){
				
				if(i == MovingBox.currentItem)	
					boxes.get(i).setBounds(MovingBox.startP.x, MovingBox.currentP.y, MovingBox.currentD.width, MovingBox.currentD.height);
			
				else
					boxes.get(i).setBounds(MovingBox.startP.x, MovingBox.startP.y, MovingBox.restD.width, MovingBox.restD.height);
				

			}else{
				if(i == MovingBox.currentItem)	
					boxes.get(i).setBounds(boxes.get(i-1).getLocation().x + boxes.get(i-1).getSize().width,
							MovingBox.currentP.y,
							MovingBox.currentD.width, MovingBox.currentD.height);
			
				else
					boxes.get(i).setBounds(boxes.get(i-1).getLocation().x + boxes.get(i-1).getSize().width,
							MovingBox.startP.y,
							MovingBox.restD.width, MovingBox.restD.height);
				
			}
		}
		
		this.getHorizontalScrollBar().setValue(boxes.get(MovingBox.currentItem).getLocation().x - MovingBox.spaceD.width);
		
	}
}
